﻿Imports System.Collections.ObjectModel
Imports System.Data.Objects

Public Class TotalConsumptionsVM
  Inherits BaseViewModel

  Private Shared mInstance As TotalConsumptionsVM

  Private WithEvents mTimer As Timers.Timer


  Private mService As AddConsumptionService
  Private mYearMinValue As Double
  Private mConsumptions As New ObservableCollection(Of DailyConsumptionVM)
  Private mFirstDate As Date = Now
  Private mSecondDate As Date = Now

  Public Property DeleteCommand As ICommand = New RelayCommand(AddressOf Delete)
  Public Property CheckDateTimeCommand As ICommand = New RelayCommand(AddressOf CheckDateTime)
  Public Property AddConsumptionCommand As ICommand = New RelayCommand(AddressOf Add, AddressOf CanAdd)
  Public Property SelectedItem As DailyConsumptionVM
  Public Property SaveCommand As ICommand = New RelayCommand(AddressOf Save)
  Public Property CurrentYear As Integer = 0
  Public Property LastConsumption As DailyConsumptionVM
  Public Property NewConsumption As Double

  Private mComment As String
  Public Property Comment() As String
    Get
      Return mComment
    End Get
    Set(ByVal value As String)
      mComment = value
    End Set
  End Property

  Public Shared ReadOnly Property Instance As TotalConsumptionsVM
    Get
      If mInstance Is Nothing Then mInstance = New TotalConsumptionsVM
      Return mInstance
    End Get
  End Property
  Public ReadOnly Property YearMinValue() As Double
    Get
      Return (From lCon In Consumptions.Where(Function(f) f.Year = CurrentYear) Select lCon.Consumption).DefaultIfEmpty(0).Min '.DefaultIfEmpty(0).Min(Function(f) f.Consumption)
    End Get
  End Property
  Public Property Consumptions() As ObservableCollection(Of DailyConsumptionVM)
    Get
      Return mConsumptions
    End Get
    Set(ByVal value As ObservableCollection(Of DailyConsumptionVM))
      mConsumptions = value
    End Set
  End Property

  Public Property FirstDate() As Date
    Get
      Return mFirstDate
    End Get
    Set(ByVal value As Date)
      mFirstDate = value
      OnPropertyChanged("ConsumptionsByDate")
      OnPropertyChanged("YearMinValueByDate")
    End Set
  End Property

  Public Property SecondDate() As Date
    Get
      Return mSecondDate
    End Get
    Set(ByVal value As Date)
      mSecondDate = value
      OnPropertyChanged("ConsumptionsByDate")
      OnPropertyChanged("YearMinValueByDate")
    End Set
  End Property

  Public ReadOnly Property ConsumptionsByDate() As List(Of DailyConsumptionVM)
    Get
      Return ConsumptionsByUser.Where(Function(f) f.DateDC >= FirstDate AndAlso f.DateDC <= SecondDate).ToList
    End Get
  End Property
  Public ReadOnly Property YearMinValueByDate() As Double
    Get
      Return (From lCon In ConsumptionsByDate Select lCon.Consumption).DefaultIfEmpty(0).Min
    End Get
  End Property

  Public Function Save()
    DB.Context.SaveChanges()
  End Function

  Public Function CanAdd()
    Return True
  End Function

  Private Sub CheckDateTime(ByVal lbEnabled As Boolean)
    If lbEnabled Then
      SelectedDateTime = Now
    Else
      SelectedDateTime = Nothing
    End If
  End Sub

  Private mDateTimeManualEdit As Boolean
  Public Property DateTimeManualEdit() As Boolean
    Get
      Return mDateTimeManualEdit
    End Get
    Set(ByVal value As Boolean)
      mDateTimeManualEdit = value
    End Set
  End Property

  Private mSelectedDateTime As DateTime
  Public Property SelectedDateTime() As DateTime
    Get
      Return mSelectedDateTime
    End Get
    Set(ByVal value As DateTime)
      mSelectedDateTime = value
      OnPropertyChanged("SelectedDateTime")
    End Set
  End Property

  Public Sub Add(ByVal lNewConsumption As String)
    Consumptions.Add(mService.AddConsumption(lNewConsumption, SelectedUser, SelectedDateTime, Comment))
    LastConsumption = Consumptions.Last
    OnPropertyChanged("LastConsumption")
    OnPropertyChanged("ConsumptionsByUser")
    OnPropertyChanged("ConsumptionsByDate")
    OnPropertyChanged("ConsumptionsByUserInverted")
  End Sub

  Private Sub Delete()
    SelectedItem.DeleteFromDB()
    Consumptions.Remove(SelectedItem)
    OnPropertyChanged("ConsumptionsByUser")
    OnPropertyChanged("ConsumptionsByDate")
    OnPropertyChanged("ConsumptionsByUserInverted")
  End Sub

  Public Sub New()
    mService = New AddConsumptionService
    mTimer = New Timers.Timer(1000)
    mTimer.AutoReset = True
    mTimer.Start()
    CurrentYear = 2012
    Dim lData As DailyConsumption

    If Not IsInDesignModeStatic Then

      lData = DB.Context.DailyConsumption.OrderByDescending(Function(f) f.DateDC).FirstOrDefault
      If lData Is Nothing Then
        lData = New DailyConsumption
        lData.Consumption = 0
        lData.DayOfYear = 0
      End If
      LastConsumption = New DailyConsumptionVM(lData)
      OnPropertyChanged("LastConsumption")
      NewConsumption = LastConsumption.Consumption
      OnPropertyChanged("NewConsumption")
      DB.Context.DailyConsumption.OrderBy(Function(f) f.DateDC).ToList.ForEach(Sub(f) Consumptions.Add(New DailyConsumptionVM(f)))

      SelectedDateTime = Now
      SecondDate = (Now + New TimeSpan(24, 0, 0))
      FirstDate = (Now - New TimeSpan(48, 0, 0))

      With DB.Context.UserCode.Where(Function(f) f.Name = My.Settings.DefaultUser)
        If .FirstOrDefault IsNot Nothing Then
          SelectedUser = .FirstOrDefault
        End If
      End With
    End If
  End Sub

  Public ReadOnly Property UserList() As List(Of UserCode)
    Get
      Return DB.Context.UserCode.ToList
    End Get
  End Property

  Public ReadOnly Property ConsumptionsByUser As List(Of DailyConsumptionVM)
    Get
      Return Consumptions.Where(Function(f) f.UserCode.IDUser = SelectedUser.IDUser).ToList
    End Get
  End Property

  Public ReadOnly Property ConsumptionsByUserInverted As List(Of DailyConsumptionVM)
    Get
      Return Consumptions.Where(Function(f) f.UserCode.IDUser = SelectedUser.IDUser).OrderByDescending(Function(f) f.DateDC).ToList
    End Get
  End Property

  Private mSelectedUser As UserCode = UserList.First
  Public Property SelectedUser() As UserCode
    Get
      Return mSelectedUser
    End Get
    Set(ByVal value As UserCode)
      mSelectedUser = value
      OnPropertyChanged("ConsumptionsByUser")
      OnPropertyChanged("ConsumptionsByDate")
      OnPropertyChanged("ConsumptionsByUserInverted")
    End Set
  End Property

  Private Sub mTimer_Elapsed(sender As Object, e As Timers.ElapsedEventArgs) Handles mTimer.Elapsed
    If Not DateTimeManualEdit Then
      SelectedDateTime = Now
    End If
  End Sub
End Class
